import Vue from 'vue'
import Vuex from 'vuex'
import apiConfig from "@/api/apiConfig"
import {
  getUser,
  getCookie,
  accountCenterInfo,
  getClientIp
} from '@/api/common'
Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    memberInfo: {},
    userInfo: {},
    curVipType: '',
    token: "",
    pointers: '--', //积分
    avatar: "",
    account: 5,
    authenticationStatus: 1, //教师共认证状态
    clientIp:'',//客户端ip
  },
  mutations: {
    getUserInfo(state, val) {debugger
      let params = "";
      if (window.location.host.indexOf("192") >= 0) {
        params = apiConfig.DEV_Token
      } else {
        params = getCookie(apiConfig['enveriment'])
      }
      getUser(params).then((res) => {
        state.userInfo = {
          ...res.Content
        };
      })
    },
    getAccountInfo(state, val) {
      state.userInfo.College=val.User.College;
      state.userInfo.ExEducation=val.User.ExEducation;
      state.userInfo.ExOccupation=val.User.ExOccupation;
      state.userInfo.ExOrgname=val.User.ExOrgname;
      state.userInfo.RealName=val.User.RealName;
      state.userInfo.Tel=val.User.Tel;
      state.userInfo.ExMajor=val.User.ExMajor;
      state.userInfo.QQ=val.User.QQ;
      state.userInfo.Mobile=val.User.Mobile;
      state.userInfo.UserName=val.User.UserName;
      state.userInfo.NickName=val.User.NickName;
      // state.userInfo = {
      //   ...val.User
      // };
      // state.memberInfo=...val.
      let timer = null;
      if (val.RewardPoints == -1) {
        timer = setInterval(function () {
          if (state.account == 0) {
            clearInterval(timer);
          } else {
            state.account--;
            let params = state.userInfo.UserId;
            accountCenterInfo(params).then((res) => {
              if ((res.Content.RewardPoints == -1) && (state.account == 0)) {
                state.pointers = "--";
              } else {
                state.pointers = res.Content.RewardPoints;
              }
            })
          }
        }, 2000)
      } else {
        if (val.RewardPoints == -1) {
          state.pointers = "--";
        } else {
          state.pointers = val.RewardPoints;
        }
      }
    },
    getClientIpState(state,ip){
      state.clientIp=ip
    }
  },
  actions: {
    getAccountCenterInfo({
      commit,
      state
    }) {
      let params = state.userInfo.UserId;
      accountCenterInfo(params).then((res) => {
        commit("getAccountInfo", res.Content)
      })
    },
    getClientIPHTTP({commit,state}){
      return new Promise(()=>{
        getClientIp().then(res=>{
          commit("getClientIpState",res.Data)
        })
      })
    }
  },
  getters:{
    isClientIp:state=>{
      return state.clientIp;
    }
  }
})